package com.sscaipiao.android.activity;

import java.io.IOException;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.sscaipiao.android.R;
import com.sscaipiao.android.activity.Base.BaseUIActivity;
import com.sscaipiao.android.activity.service.NotificationService;
import com.sscaipiao.android.common.Constants;
import com.sscaipiao.android.model.User;
import com.sscaipiao.android.utils.ServiceUtil;
import com.sscaipiao.android.utils.StringUtil;
import com.sscaipiao.android.utils.UserDataServiceHelper;

public class LoginActivity extends BaseUIActivity implements OnClickListener {

	private Button loginButton;
	private Button testButton;
	private EditText userName;
	private EditText password;
	private TextView loginErrorView;
	private TextView register;
	private TextView forgetPassword;
	private ImageView sinaLogin;
	private ImageView qqLogin;
	private ImageView paypalLogin;

	@Override
	public void onCreate(Bundle savedInstanceState) {

		StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
				.detectDiskReads().detectDiskWrites().detectNetwork()
				.penaltyLog().build());
		StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
				.detectLeakedSqlLiteObjects().detectLeakedClosableObjects()
				.penaltyLog().penaltyDeath().build());

		super.onCreate(savedInstanceState);
		//setContentView(R.layout.login);
		addContainerMain(R.layout.login);
		setTitleText(R.string.login);
		setupUI(false,false,false,false,false);
		loginButton = (Button) findViewById(R.id.button_login);
		userName = (EditText) findViewById(R.id.txt_username);
		password = (EditText) findViewById(R.id.txt_password);
		loginErrorView = (TextView) findViewById(R.id.txt_lognierror);
		register = (TextView) findViewById(R.id.txt_toregister);
		forgetPassword = (TextView) findViewById(R.id.txt_forget_password);
		sinaLogin = (ImageView) findViewById(R.id.sina_login_icon);
		qqLogin = (ImageView) findViewById(R.id.qq_login_icon);
		paypalLogin = (ImageView) findViewById(R.id.paypal_login_icon);

		loginButton.setOnClickListener(this);
		register.setOnClickListener(this);
		forgetPassword.setOnClickListener(this);
		sinaLogin.setOnClickListener(this);
		qqLogin.setOnClickListener(this);
		paypalLogin.setOnClickListener(this);
		testButton = (Button) findViewById(R.id.button_test);
		testButton.setOnClickListener(this);
	}

	@Override
	public void onResume() {
		super.onResume();
		//userName.setText("lliangwh");
		//password.setText("a12345678");

		userName.setText("Jiutian");
		password.setText("111111");
		loginButton.performClick();


	}

	@Override
	public void onClick(View v) {
		if (R.id.button_login == v.getId()) {
			showProgressDialog();
			Thread thr = new Thread(mTask, "1");
			thr.start();


		} else if (R.id.txt_toregister == v.getId()) {
			// 创建注册对话框
			Log.v("txt_toregister", "txt_toregister");

			Intent intent = new Intent(LoginActivity.this, RegistActivity.class);
			Bundle b = new Bundle();
			intent.putExtras(b);
			startActivity(intent);
		} else if (R.id.txt_forget_password == v.getId()) {
			Intent intent = new Intent(LoginActivity.this,ForgetPasswordActivity.class);
			Bundle b = new Bundle();
			intent.putExtras(b);
			startActivity(intent);
		} else if (R.id.sina_login_icon == v.getId()) {
			Toast.makeText(LoginActivity.this, "尚未支持",Toast.LENGTH_SHORT).show();
		} else if (R.id.qq_login_icon == v.getId()) {
			Toast.makeText(LoginActivity.this, "尚未支持",Toast.LENGTH_SHORT).show();
		} else if (R.id.paypal_login_icon == v.getId()) {

		} else if (R.id.button_test == v.getId()) {
			startActivity(new Intent().setClass(this, TestStationActivity.class));
		} else {

			return;
		}
	}

	private void doLogin() {
		// setContentView(R.layout.regist1);
		String name = String.valueOf(userName.getText());
		String pass = String.valueOf(password.getText());
		Log.v("LoginActivity.login", name + " " + pass);
		if (StringUtil.isNotEmptyString(name) && StringUtil.isNotEmptyString(pass)) {
			try {
				User user = UserDataServiceHelper.login(LoginActivity.this, name, pass);
				if (user == null) {
					loginErrorView.setVisibility(View.VISIBLE);
					return;
				}
				MyApplication appState = (MyApplication) getApplicationContext();
				appState.setGod(user);
				if (StringUtil.isNotEmptyString(user.getToken()) && user.getStatus().equals(Constants.LOGIN_SUCCESS_CODE)) {
					// go to hall
					SharedPreferences properties = getSharedPreferences("properties", 0);
					if (properties.getString("needAlert", "").equals("") || properties.getString("needAlert", "").equals("false")) {

					} else if (!ServiceUtil.isServiceRunning(this, "NotificationService")) {
						Intent i = new Intent();
						Bundle b = new Bundle();
						if (user == null) {
							user = new User();
						}
						//user.setToken(retUser.getToken());
						b.putSerializable(Constants.USER, user);
						i.putExtras(b);
						i.setClass(LoginActivity.this, NotificationService.class);
						startService(i);
					}
					closeProgressDailog();
					Intent intent = new Intent(LoginActivity.this, HallActivity.class);
//						Bundle b = new Bundle();
//						if (user == null) {
//							user = new User();
//						}
//						//user.setToken(retUser.getToken());
//						b.putSerializable(Constants.USER, user);
//						intent.putExtras(b);
					startActivity(intent);

				} else {
					loginErrorView.setText(user.getMessage());
					loginErrorView.setVisibility(View.VISIBLE);
					closeProgressDailog();
				}

			} catch (IOException e) {
				Log.v("LoginActivity.login", e.toString());
				runOnUiThread(new Runnable() {
					@Override
					public void run() {
						Toast.makeText(LoginActivity.this, "网络连接失败",Toast.LENGTH_SHORT).show();
					}
				});
				closeProgressDailog();
			}
		} else {
			closeProgressDailog();
		}
	}

	Runnable mTask = new Runnable() {
		public void run() {
			doLogin();
		}
	};

}
